package hos.intent;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import java.util.ArrayList;

/**
 * <p>Title: SettingCompat </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-20 20:37
 */
public class IntentStart {
    //<editor-fold desc="Open">

    /**
     * 打开定位设置页面
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openLocationSetting(Context context, int requestCode) {
        Intent intent = IntentCollection.getLocationSetting();
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开写入设置页面
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openWriteSetting(Context context, int requestCode) {
        Intent intent = IntentCollection.getWriteSettings(context);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开系统弹窗
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openSystemAlert(Context context, int requestCode) {
        Intent intent = IntentCollection.getSystemAlertSetting(context);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开应用安装
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openInstallSetting(Context context, int requestCode) {
        Intent intent = IntentCollection.getInstallSetting(context);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开通知设置
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openNotificationSetting(Context context, int requestCode) {
        Intent intent = IntentCollection.getNotificationSetting(context);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开储存设置
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openStorageSetting(Context context, int requestCode) {
        Intent intent = IntentCollection.getStorageSetting(context);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开系统设置
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openSystemConfig(Context context, int requestCode) {
        Intent intent = IntentCollection.getSystemConfigSetting();
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开应用详情设置
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean openSetting(Context context, int requestCode) {
        Intent intent = IntentCollection.getSetting(context);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开浏览器
     *
     * @param context     上下文
     * @param url         访问地址
     * @param requestCode 请求码
     */
    public static boolean openUrl(Context context, String url, int requestCode) {
        Intent intent = IntentCollection.getUrl(url);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打开应用市场
     *
     * @param context     上下文
     * @param packageName 需要下载的APK
     * @param requestCode 请求码
     */
    public static boolean openMarket(Context context, String packageName, int requestCode) {
        Intent intent = IntentCollection.getMarket(packageName);
        return IntentCollection.startResult(context, intent, requestCode);
    }
    //</editor-fold>

    //<editor-fold desc="Install">

    /**
     * 获取安装APK的意图
     *
     * @param uri 安装APK的URI
     */
    public static boolean launchInstallApp(Context context, final Uri uri, int requestCode) {
        Intent intent = IntentCollection.getInstallAppIntent(uri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 卸载APK的意图
     *
     * @param pkgName 卸载apk的包名
     */
    public static boolean launchUninstall(Context context, final String pkgName, int requestCode) {
        Intent intent = IntentCollection.getUninstallAppIntent(pkgName);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 获取需要启动的APK的意图
     *
     * @param context 上下文
     * @param pkgName 包名
     */
    public static boolean launchApp(Context context, final String pkgName, int requestCode) {
        Intent intent = IntentCollection.getLaunchAppIntent(context, pkgName);
        return IntentCollection.startResult(context, intent, requestCode);
    }
    //</editor-fold>


    //<editor-fold desc="Phone">

    /**
     * 调用拨号面板
     *
     * @param phoneNumber 电话号码
     */
    public static boolean calPhoneDial(Context context, String phoneNumber, int requestCode) {
        Intent intent = IntentCollection.getDialIntent(phoneNumber);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 打电话
     *
     * @param phoneNumber 电话号码
     */
    public static boolean calPhone(Context context, final String phoneNumber, int requestCode) {
        Intent intent = IntentCollection.getCallIntent(phoneNumber);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 发送短信
     *
     * @param phoneNumber 电话号码
     * @param content     短信内容
     */
    public static boolean sendSms(Context context, final String phoneNumber,
                                  final String content, int requestCode) {
        Intent intent = IntentCollection.getSendSmsIntent(phoneNumber, content);
        return IntentCollection.startResult(context, intent, requestCode);
    }
    //</editor-fold>

    //<editor-fold desc="Share">

    /**
     * 分享文字
     *
     * @param content 内容
     */
    public static boolean shareTextIntent(Context context, final String content, int requestCode) {
        Intent intent = IntentCollection.getShareTextIntent(content);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 分享图片
     *
     * @param imageUri 图片地址
     */
    public static boolean shareTextImageIntent(Context context, final Uri imageUri, int requestCode) {
        Intent intent = IntentCollection.getShareTextImageIntent(imageUri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 分享文字和图片
     *
     * @param content  内容
     * @param imageUri 图片地址
     */
    public static boolean shareTextImageIntent(Context context, final String content,
                                               final Uri imageUri, int requestCode) {
        Intent intent = IntentCollection.getShareTextImageIntent(content, imageUri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 分享文字和图片
     *
     * @param content 内容
     * @param uris    图片地址列表
     */
    public static boolean shareTextImageIntent(Context context, final String content,
                                               final ArrayList<Uri> uris, int requestCode) {
        Intent intent = IntentCollection.getShareTextImageIntent(content, uris);
        return IntentCollection.startResult(context, intent, requestCode);
    }
    //</editor-fold>

    //<editor-fold desc="File">
    public static boolean openFile(Context context, Uri uri,
                                   String type, int requestCode) {
        Intent intent = IntentCollection.getFileIntent(uri, type);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    public static boolean openFile(Context context, String pkgName,
                                   Uri uri, String type, int requestCode) {
        Intent intent = IntentCollection.getFileIntent(context, pkgName, uri, type);
        return IntentCollection.startResult(context, intent, requestCode);
    }
    //</editor-fold>

    //<editor-fold desc="Media">

    /**
     * 媒体意图
     *
     * @param action 行为
     * @param uri    访问地址
     */
    public static boolean createMedia(Context context, String action, Uri uri, int requestCode) {
        Intent intent = IntentCollection.getMediaIntent(action, uri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 拍照意图
     *
     * @param uri 访问地址
     */
    public static boolean createImage(Context context, Uri uri, int requestCode) {
        Intent intent = IntentCollection.getImageMedia(uri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 录像意图
     *
     * @param uri 访问地址
     */
    public static boolean createVideo(Context context, Uri uri, int requestCode) {
        Intent intent = IntentCollection.getVideoMedia(uri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 录音意图
     *
     * @param uri 访问地址
     */
    public static boolean createAudio(Context context, Uri uri, int requestCode) {
        Intent intent = IntentCollection.getAudioMedia(uri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 媒体意图
     */
    public static boolean choosePick(Context context, String type, int requestCode) {
        Intent intent = IntentCollection.getPickMedia(type);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 图片意图
     */
    public static boolean choosePickImage(Context context, int requestCode) {
        Intent intent = IntentCollection.getImagePickMedia();
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 视频意图
     */
    public static boolean choosePickVideo(Context context, int requestCode) {
        Intent intent = IntentCollection.getVideoPickMedia();
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 联系人意图
     */
    public static boolean chooseContacts(Context context, int requestCode) {
        Intent intent = IntentCollection.getContacts();
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 裁剪意图
     */
    public static boolean createCrop(Context context, Uri inputUri, Uri outputUri, int requestCode) {
        Intent intent = IntentCollection.getCrop(inputUri, outputUri);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 裁剪意图
     */
    public static boolean createCrop(Context context, Uri inputUri, Uri outputUri,
                                     int outputX, int outputY, int requestCode) {
        Intent intent = IntentCollection.getCrop(inputUri, outputUri, outputX, outputY);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 裁剪意图
     */
    public static boolean createCrop(Context context, Uri inputUri, Uri outputUri,
                                     int aspectX, int aspectY,
                                     int outputX, int outputY, int requestCode) {
        Intent intent = IntentCollection.getCrop(inputUri, outputUri, aspectX, aspectY, outputX, outputY);
        return IntentCollection.startResult(context, intent, requestCode);
    }
    //</editor-fold>

    //<editor-fold desc="Content">

    /**
     * 单选
     *
     * @param type 文件的类型
     */
    public static boolean chooseContent(Context context, String type, int requestCode) {
        Intent intent = IntentCollection.getContentIntent(type);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 多选
     *
     * @param type 文件的类型
     */
    public static boolean chooseContentList(Context context, String type, int requestCode) {
        Intent intent = IntentCollection.getContentListIntent(type);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 单选所有
     */
    public static boolean chooseContentAll(Context context, int requestCode) {
        return chooseContent(context, "*/*", requestCode);
    }

    /**
     * 多选所有
     */
    public static boolean chooseContentListAll(Context context, int requestCode) {
        return chooseContentList(context, "*/*", requestCode);
    }

    /**
     * 单选图片
     */
    public static boolean chooseContentImage(Context context, int requestCode) {
        return chooseContent(context, "image/*", requestCode);
    }

    /**
     * 多选图片
     */
    public static boolean chooseContentListImage(Context context, int requestCode) {
        return chooseContentList(context, "image/*", requestCode);
    }

    /**
     * 单选视频
     */
    public static boolean chooseContentVideo(Context context, int requestCode) {
        return chooseContent(context, "video/*", requestCode);
    }

    /**
     * 多选视频
     */
    public static boolean chooseContentListVideo(Context context, int requestCode) {
        return chooseContentList(context, "video/*", requestCode);
    }
    //</editor-fold>


    //<editor-fold desc="Document">

    /**
     * 单选
     *
     * @param type 文件的类型
     */
    public static boolean chooseDocument(Context context, int requestCode, String... type) {
        Intent intent = IntentCollection.getDocumentIntent(type);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 多选
     *
     * @param type 文件的类型
     */
    public static boolean chooseDocumentList(Context context, int requestCode, String... type) {
        Intent intent = IntentCollection.getDocumentListIntent(type);
        return IntentCollection.startResult(context, intent, requestCode);
    }

    /**
     * 单选所有
     */
    public static boolean chooseDocumentAll(Context context, int requestCode) {
        return chooseDocument(context, requestCode, "*/*");
    }

    /**
     * 多选所有
     */
    public static boolean chooseDocumentListAll(Context context, int requestCode) {
        return chooseDocumentList(context, requestCode, "*/*");
    }

    /**
     * 单选图片
     */
    public static boolean chooseDocumentImage(Context context, int requestCode) {
        return chooseDocument(context, requestCode, "image/*");
    }

    /**
     * 多选图片
     */
    public static boolean chooseDocumentListImage(Context context, int requestCode) {
        return chooseDocumentList(context, requestCode, "image/*");
    }

    /**
     * 单选视频
     */
    public static boolean chooseDocumentVideo(Context context, int requestCode) {
        return chooseDocument(context, requestCode, "video/*");
    }

    /**
     * 多选视频
     */
    public static boolean chooseDocumentListVideo(Context context, int requestCode) {
        return chooseDocumentList(context, requestCode, "video/*");
    }

    /**
     * 单选图片和视频
     */
    public static boolean chooseDocumentImageVideo(Context context, int requestCode) {
        return chooseDocument(context, requestCode, "image/*", "video/*");
    }

    /**
     * 多选图片和视频
     */
    public static boolean chooseDocumentListImageVideo(Context context, int requestCode) {
        return chooseDocumentList(context, requestCode, "image/*", "video/*");
    }
    //</editor-fold>

    //<editor-fold desc="CheckDialog">

    /**
     * 打开定位设置页面
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean checkLocationSetting(Context context, int requestCode) {
        if (IntentAvailable.locationAvailable(context)) {
            return true;
        }
        IntentAvailable.showSettingTip(context, "需要打开系统定位开关",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openLocationSetting(context, requestCode);
                    }
                });
        return false;
    }

    /**
     * 打开写入设置页面
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean checkWriteSetting(Context context, int requestCode) {
        if (IntentAvailable.writeSettingAvailable(context)) {
            return true;
        }
        IntentAvailable.showSettingTip(context, "需要打开写入设置开关",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openWriteSetting(context, requestCode);
                    }
                });
        return false;
    }

    /**
     * 打开系统弹窗
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean checkSystemAlert(Context context, int requestCode) {
        if (IntentAvailable.systemDialogAvailable(context)) {
            return true;
        }
        IntentAvailable.showSettingTip(context, "需要打开系统弹窗开关",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openSystemAlert(context, requestCode);
                    }
                });
        return false;
    }

    /**
     * 打开应用安装
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean checkInstallSetting(Context context, int requestCode) {
        if (IntentAvailable.installAvailable(context)) {
            return true;
        }
        IntentAvailable.showSettingTip(context, "需要打开应用安装开关",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openInstallSetting(context, requestCode);
                    }
                });
        return false;
    }

    /**
     * 打开通知设置
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean checkNotificationSetting(Context context, int requestCode) {
        if (IntentAvailable.notificationAvailable(context)) {
            return true;
        }
        IntentAvailable.showSettingTip(context, "需要打开应用通知开关",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openNotificationSetting(context, requestCode);
                    }
                });
        return false;
    }

    /**
     * 打开储存设置
     *
     * @param context     上下文
     * @param requestCode 请求码
     */
    public static boolean checkStorageSetting(Context context, int requestCode) {
        if (IntentAvailable.storageAvailable()) {
            return true;
        }
        IntentAvailable.showSettingTip(context, "需要打开应用储存知开关",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openStorageSetting(context, requestCode);
                    }
                });
        return false;
    }

    /**
     * 打开应用市场
     *
     * @param context     上下文
     * @param packageName 需要下载的APK
     * @param requestCode 请求码
     */
    public static void checkMarket(Context context, String packageName, int requestCode) {
        IntentAvailable.showSettingTip(context, "即将打开应用市场，您确定要打开吗？",
                "打开", "取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openMarket(context, packageName, requestCode);
                    }
                });
    }

    /**
     * 打开浏览器
     *
     * @param context     上下文
     * @param url         访问地址
     * @param requestCode 请求码
     */
    public static void checkUrl(Context context, String url, int requestCode) {
        IntentAvailable.showSettingTip(context, "即将打开浏览器，您确定要打开吗？",
                "打开", "取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openUrl(context, url, requestCode);
                    }
                });
    }
    //</editor-fold>

}
